<?php

/***  DOCUMENTATION LAYER

Klenwell Ceo Form Element Textarea Class

Name: CeoFormElementTextarea
Last Update: Sep 2007
Author: Tom at klenwell@gmail.com

DESCRIPTION
  CEO form element class for textarea fields

METHODS
  MAGIC
  CeoFormElementInputText($name, $type, $label, $is_required, $maxlength, $minlength)    *php 4 constructor*
  __construct($name, $type, $label, $is_required, $maxlength, $minlength)    *php 5 constructor*
  __destruct()  
  
  PUBLIC
  
  PRIVATE
  _is_valid_url_strict($url)
  _set_session_data()
  _get_session_data()
  _has_session_data()
  _set_filename()
  _set_dirpath()
  
USAGE
  $Class = new ClassClass();
  $Class->print_r('hello world']);

NOTES

______________________________________________________________________________*/

// Load File of Base Class


// Class
/*____________________________________________________________________________*/
class CeoFormElementTextarea extends CeoFormElement
{
/* PUBLIC PROPERTIES */
var $debug = 0;
var $class_name = __CLASS__;
var $oid = '';
var $DS = DIRECTORY_SEPARATOR;

// path settings
var $HtmlValidator_path = '';
var $HtmlValidator_fname = 'html_validator.class.php';

// control arrays
var $VALID = array();
var $WHITE = array();

// basic arguments
var $type = '';
var $name = '';
var $label = '';
var $is_required = 0;
var $maxwords = 250;
var $minwords = 0;
var $maxlen = 2000;
var $cols = 40;
var $rows = 8;

// public properties
var $valid_input = null;
var $class = '';
var $id = '';
var $style = '';

/* PRIVATE PROPERTIES */
var $_HtmlValidator = 0;
var $_value = null;
var $_prompt = '';
var $_html_signal = '';

var $_filename = '';
var $_dirpath = '';


/* ** MAGIC METHODS ** */
// php4 constructor
function CeoFormElementTextarea($name, $type, $label, $is_required, $maxwords=250, $minwords=0, $WHITE_TAG=null, $WHITE_ATT=null, $debug=0)
{
  $this->__construct($name, $type, $label, $is_required, $maxwords, $minwords, $WHITE_TAG, $WHITE_ATT, $debug);
  register_shutdown_function( array($this, '__destruct') );
}
// END constructor

// php5 constructor
function __construct($name, $type, $label, $is_required, $maxwords, $minwords, $WHITE_TAG, $WHITE_ATT, $debug)
{
  // default
  $this->oid = $type . $name;
  $this->debug = $debug;
  $this->_set_filename();
  $this->_set_dirpath();
  
  // set parameters
  $this->HtmlValidator_dir  = dirname(dirname($this->_dirpath)) . $this->DS . "ext_oss{$this->DS}klenwell" . $this->DS;
  $this->HtmlValidator_path = $this->HtmlValidator_dir . $this->HtmlValidator_fname;
  
  // set allowable tags
  global $CEO;
  $this->WHITE['TAG'] = ( is_null($WHITE_TAG) ) ? $CEO['IN']['WHITE']['TAG'] : $WHITE_TAG;
  $this->WHITE['ATT'] = ( is_null($WHITE_ATT) ) ? $CEO['IN']['WHITE']['ATT'] : $WHITE_ATT;
    
  // additional props
  $this->maxwords = $maxwords;
  $this->minwords = $minwords;
  $this->maxlen   = $this->maxwords * 8;
  
  // call parent constructor
  parent::__construct($name, $type, $label, $is_required, $maxwords, $minwords, $WHITE_TAG, $WHITE_ATT);

  // override parent values
  $this->VALID['TYPE'] = array( 'textarea' );  
}
// END constructor

// destructor
function __destruct()
{
  if ( $this->debug ) $this->print_d("destroying class {$this->class_name} (oid: {$this->oid})");
}
// END destructor



/* ** PUBLIC METHODS ** */
// method: print as block
function print_block($cols=40, $rows=8, $js_validate=1)
{
  $html = '';    // return
  
  // set attributes
  $js_validator = '';
  $js_event = '';
  
  // js validator
  if ( $js_validate )
  {
    $js_validator = <<<JS
  
<script language="javascript">
function js_maxlength_{$this->name}(e, obj, maxLength)
{
	var regex = new RegExp("\\r\\n","g");
	var iText;
	
	// get keycode
	var iKey = ( e.keyCode != 0 ) ? e.keyCode : e.which;
	if ( 0 ) alert("iKey = " + iKey); // debug
	
	// get length
	textLength = obj.value.replace(regex,"").length;
	if ( 0 ) alert("iText length " + textLength); // debug (will alert at every keypress!)
	
	// check length
	if ( textLength >= maxLength && ( iKey != 46 && iKey != 8 ) )
	{
		alert("too long : please limit input to about {$this->maxwords} words");
		obj.value = obj.value.substring(0, maxLength-2);
		return false;
	}

	return;
}
</script>
  
JS;
  }
  
  if ( $js_validate ) $js_event = ' onkeypress="js_maxlength_'.$this->name.'(event, this, '.$this->maxlen.')"';
  
  // set prompt class
  $_prompt_class = ( $this->is_valid ) ? $this->valid_class_name : $this->invalid_class_name;
  
  $html_prompt = ( $this->is_submitted() && !empty($this->_prompt) ) ? "<div class=\"prompt\"><span class=\"$_prompt_class\">{$this->_prompt}</span></div>" : '';
  $html_signal = "<input type=\"hidden\" name=\"{$this->_html_signal}\" value=\"1\"  />";
  $value = $this->_get_display_value();
  
  $core_html = <<<HTML

<!-- JS: textarea validator -->
$js_validator

 <div class="label"><label for="{$this->name}">{$this->label}</label></div>
 {$html_prompt}
 
 <textarea name="$this->name" rows="$rows" cols="$cols"{$js_event}>{$value}</textarea> 
 {$html_signal}
HTML;

  // wrap element
  $html = $this->_wrap_print_block($core_html, $this->type, $this->name, $this->id, $this->class);

  return $html;
}


// method: second order validator (must be called explicitly)
function validate2($is_white, $TAGS, $ATTS, $maxwords=null, $minwords=null, $maxlen=null, $minlen=null)
{
  trigger_error('in dev', E_USER_WARNING);
}
// END method


// method: filter input
function filter_input($text, $debug=0)
{
  $sanitized_text = '';		// return
  if ( $this->debug ) $this->print_d('filtering input');
	$sanitized_text = CeoForm::staticInputFilter($text, $this->WHITE['TAG'], $this->WHITE['ATT']);

	return $sanitized_text;
}
// END method



/* ** PRIVATE METHODS ** */
// method: validate input
function _validate_input()
{
  $_fail = 0;
  $_prompt = '';
  
  if ( $this->debug ) $this->print_d(__FUNCTION__ . ": element '{$this->name}'");
  
  // clear valid value
  $this->valid_input = '';
  
  // validation data
  $_strlen = strlen($this->_value);
  $_num_words = str_word_count($this->_value);
  
  // required
  if ( $this->is_required && empty($this->_value) )
  {
    $_prompt = 'required value &ndash; please fill in';
    $_fail = 1;
  }
  
  // too few words
  if ( !$_fail && $this->is_required && ( $_num_words < $this->minwords ) )
  {
    $_prompt = "this is too short (must be at least {$this->minwords} words)";
    $_fail = 1;
  }
  
  // too long
  if ( !$_fail && ( $_strlen > $this->maxlen ) )
  {
    $_prompt = "this is too long (please limit to {$this->maxlen} chars)";
    $_fail = 1;
  }
  
  // too many words
  if ( !$_fail && ( $_num_words > $this->maxwords ) )
  {
    $_prompt = "this is too long (please no more than {$this->maxwords} words)";
    $_fail = 1;
  }
  
  // valid html
  if ( !$_fail && !empty($this->_value) )
  {
    $this->_load_html_validator();
    $this->_HtmlValidator->validate($this->_value);
    if ( !$this->_HtmlValidator->is_valid() )
    {
      $_prompt = $this->_HtmlValidator->get_prompt('There was a problem with your text.');
      $_fail = 1;
    }
  }
  
  // failed?
  if ( !$_fail )
  {
    $this->is_valid = 1;
    $this->valid_input = $this->_value;
  }
  else
  {
    $this->is_valid = 0;
    $this->_prompt = $_prompt;    
  }
  
 
  // debug
  if ( $this->debug ) $this->print_d("is valid: {$this->is_valid} -> prompt: {$this->_prompt}");
  
  return;
}
// END method


// method: load html validator
function _load_html_validator()
{
  // already loaded
  if ( is_object($this->_HtmlValidator) ) 
  {
    if ( $this->debug ) $this->print_d('HtmlValidator already loaded');
    return;
  }
  
  if ( $this->debug ) $this->print_d('loading HtmlValidator');
  require_once($this->HtmlValidator_path);
  $this->_HtmlValidator = new HtmlValidator($this->debug);
  return;
}
// END method


// method: is valid url (strict)
// statically calls framework method
function _is_valid_url_strict($url)
{
  return CeoFramework::is_valid_url_strict($url);
}

function _set_filename() { $this->_filename = basename(__FILE__); }
function _set_dirpath() { $this->_dirpath = dirname(__FILE__) . $this->DS; }

} // end class
/*____________________________________________________________________________*/

?>
